#include <stdio.h>
#define MAXLINE 1000

int getline2(char line[], int maxline);
void doline(char to[], char from[], int len);

int main()
{
    int len;
    char line[MAXLINE];
    char out[MAXLINE];

    while ((len = getline2(line, MAXLINE)) > 0)
    {
        doline(out, line, len);
        printf("linelen = %d, out = %s\n", len, out);
    }

    return 0;
}

int getline2(char line[], int maxline)
{
    int c, i;

    for (i = 0; i < maxline - 1 && (c = getchar()) != EOF && c != '\n'; i++)
    {
        line[i] = c;
    }
    if (c == '\n')
    {
        line[i] = c;
        i++;
    }
    line[i] = '\0';
    return i;
}

void doline(char to[], char from[], int len)
{
    int i = 0, j = 0;

    for(i = len - 1; i > 0; i--)
    {
        if(from[i] == ' ' || from[i] == '\t')
            j++;
        else
        {
            break;
        }        
    }

    for(i = 0; i < len - j; i++)
    {
        to[i] = from[i];
    }
    to[len - j] = '\0';    
}